using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Grafica1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        public Model cubo, model, camera;
        public Texture2D separator;

        public Game1()
        {
            this.Components.Add(new ViewPortsManager(this));
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.IsMouseVisible = true;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.

            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            separator = Content.Load<Texture2D>("separator");

            model = Content.Load<Model>("polok");
            cubo = Content.Load<Model>("cubo");
            camera = Content.Load<Model>("camera");
            var fx = Content.Load<Effect>("Phong");

            foreach (var m in model.Meshes)
            {
                foreach (var mp in m.MeshParts)
                {
                    var bf = (BasicEffect)mp.Effect;
                    bf.EnableDefaultLighting();
                    var myfx = fx.Clone();
                    myfx.Parameters["SpecularPower"].SetValue(bf.SpecularPower);
                    myfx.Parameters["SpecularColor"].SetValue(new Vector4(bf.SpecularColor, 255));
                    myfx.Parameters["DiffuseColor"].SetValue(new Vector4(bf.DiffuseColor, 255));
                    myfx.Parameters["AmbientLightColor"].SetValue(new Vector4(bf.AmbientLightColor, 255));

                    myfx.Parameters["Direction0"].SetValue(bf.DirectionalLight0.Direction);
                    myfx.Parameters["DiffuseColor0"].SetValue(new Vector4(bf.DirectionalLight0.DiffuseColor, 255));
                    myfx.Parameters["SpecularColor0"].SetValue(new Vector4(bf.DirectionalLight0.SpecularColor, 255));

                    myfx.Parameters["Direction1"].SetValue(bf.DirectionalLight1.Direction);
                    myfx.Parameters["DiffuseColor1"].SetValue(new Vector4(bf.DirectionalLight1.DiffuseColor, 255));
                    myfx.Parameters["SpecularColor1"].SetValue(new Vector4(bf.DirectionalLight1.SpecularColor, 255));

                    myfx.Parameters["Direction2"].SetValue(bf.DirectionalLight2.Direction);
                    myfx.Parameters["DiffuseColor2"].SetValue(new Vector4(bf.DirectionalLight2.DiffuseColor, 255));
                    myfx.Parameters["SpecularColor2"].SetValue(new Vector4(bf.DirectionalLight2.SpecularColor, 255));

                    


                    mp.Effect = myfx;
                }               

            }
            foreach (var m in cubo.Meshes)
            {
                ((BasicEffect)m.Effects[0]).EnableDefaultLighting();
            }
            foreach (var m in camera.Meshes)
            {
                ((BasicEffect)m.Effects[0]).EnableDefaultLighting();
            }


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
